package com.uzai.console.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.console.entity.MiniArea;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liuqi
 * @date 2023年11月10日 13:57
 */
public class AddressCode {

        public static List<MiniArea> getProvinceList(String str){

            int now = DateUtil.getNowTime();

            List<MiniArea> addressList = new ArrayList<>();

            JSONArray JSONArray = JSONObject.parseArray(str);

            for (Object object : JSONArray) {
                JSONObject province = JSONObject.parseObject(object.toString());
                MiniArea miniArea = new MiniArea();
                miniArea.setId(IdWorker.getId());
                miniArea.setCreateTime(now);
                miniArea.setUpdateTime(now);
                miniArea.setCode(province.getString("code"));
                miniArea.setName(province.getString("value"));
                miniArea.setParent(null);
                miniArea.setType("province");
                addressList.add(miniArea);

                JSONArray children1 = province.getJSONArray("children");
                for (Object cityChild : children1) {
                    MiniArea  cityArea = new MiniArea();
                    JSONObject city = JSONObject.parseObject(cityChild.toString());
                    cityArea.setCode(city.getString("code"));
                    cityArea.setName(city.getString("value"));
                    cityArea.setParent(province.getString("code"));
                    cityArea.setType("city");
                    cityArea.setId(IdWorker.getId());
                    cityArea.setCreateTime(now);
                    cityArea.setUpdateTime(now);

                    addressList.add(cityArea);


                    JSONArray children2 = city.getJSONArray("children");
                    for (Object areaChild : children2) {
                        MiniArea  areaArea = new MiniArea();
                        JSONObject area = JSONObject.parseObject(areaChild.toString());
                        areaArea.setCode(area.getString("code"));
                        areaArea.setName(area.getString("value"));
                        areaArea.setParent(city.getString("code"));
                        areaArea.setType("area");
                        areaArea.setId(IdWorker.getId());
                        areaArea.setCreateTime(now);
                        areaArea.setUpdateTime(now);

                        addressList.add(areaArea);
                    }
                }
            }

            return addressList;
        }


    public static void main(String[] args) {
        String path = "C:/Users/luche/Desktop/new 7.txt";
        FileInputStream fis = null;


        StringBuffer content = new StringBuffer("");

        try (
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)))) {
            String line;
            while (StringUtils.isNotBlank(line = br.readLine())) {
                content.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

//        content.forEach(System.out::println);
        System.out.println(content);

        List<MiniArea> maps = getProvinceList(content.toString());

        System.out.println(JSONObject.toJSONString(maps));


    }

}
